<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="queryParams" label-width="80px" inline>
        <el-form-item label="组" prop="name">
          <el-input v-model="queryParams.name" size="small"/>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <FormSelect v-model="queryParams.status" size="small"
                      :options="[{label:'可用',value:1},{label:'禁用',value: 0}]"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addFormVisible = true" size="small">添加</el-button>
          <el-button type="primary" @click="getList" size="small">查询</el-button>
          <el-button type="primary" @click="resetForm('form')" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div>
      <el-table v-loading="tableLoading"
                :data="tableData"
                height="600"
                border
      >
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="组名" prop="name"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==1">正常</el-tag>
            <el-tag type="danger" v-else>失效</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="()=>{viewVisible=true;viewData = scope.row}">查看</el-button>
            <el-button size="small" type="text" @click="setEditData(scope.row)">编辑</el-button>
            <el-button size="small" type="text" @click="()=>{checkedGroupId = scope.row.id;loadGroupRoles();}">绑定角色</el-button>
            <el-popconfirm
              confirm-button-text="好的"
              cancel-button-text="不用了"
              icon="el-icon-info"
              icon-color="red"
              @confirm="remove(scope.row)"
              title="确定删除吗？"
            >
              <el-button size="small" slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        background
        :page-sizes="pageWrapper.pageSizes"
        :total="pageWrapper.total"
        :limit.sync="pageWrapper.size"
        :page.sync="pageWrapper.current"
        auto-scroll
        @pagination="getList"
        layout="sizes,prev, pager, next"
      />
    </div>

    <!-- form  -->
    <el-dialog title="添加" :visible.sync="addFormVisible"
               @close="()=>{this.resetForm('addForm')}"
    >
      <el-form ref="addForm" label-width="80" :rules="addRules" :model="addFormData">
        <el-form-item label="组名" prop="name">
          <el-input v-model="addFormData.name" size="small"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addFormData.remark" type="textarea" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="createTGroup('addForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="editFormVisible"
               @close="()=>{this.resetForm('editForm')}"
    >
      <el-form ref="editForm" label-width="80" :rules="editRules" :model="editFormData">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editFormData.name" size="small"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editFormData.remark" type="textarea" size="small"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <FormSelect v-model="editFormData.status" :options="[{label:'可用',value:1},{label:'禁用',value:0}]"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateTGroup('editForm')">确 定</el-button>
      </div>
    </el-dialog>


    <!--view-->
    <el-dialog :visible.sync="viewVisible" footer="">
      <el-descriptions border title="查看">
        <el-descriptions-item label="id">{{ viewData.id }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ viewData.createTime }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ viewData.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="名称">{{ viewData.name }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ viewData.remark }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ viewData.status }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <el-dialog   width="30%" title="绑定角色"  :visible.sync="roleVisible" >
      <CheckBoxGroup ref="checkgroup" v-model="roles" :async-fun="allRole" :config="{labelKey:'name',valueKey:'id'}"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateGroupRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { pageData, create, update, removeById ,loadGroupRole,bindRoles } from '@/api/system/tGroup'
import { pageMixin, formMixin } from '@/mixins/mixins'
import FormSelect from '@/components/FormComponent/FormSelect'
import CheckBoxGroup from '@/components/FormComponent/CheckBoxGroup'
import {allRole} from '@/api/system/tRole'
export default {
  name: 'TGroup',
  mixins: [pageMixin, formMixin],
  components: { FormSelect, Pagination ,CheckBoxGroup},
  data() {
    return {
      tableLoading: false,
      tableData: [],
      queryParams: {
        name: '',
        status: ''
      },
      roleVisible:false,
      roles:[],
      checkedGroupId:'',
      addFormVisible: false,
      editFormVisible: false,
      viewVisible: false,
      viewData: {
        id: '',
        createTime: '',
        updateTime: '',
        name: '',
        remark: '',
        status: ''
      },
      addFormData: {
        id: '',
        createTime: '',
        updateTime: '',
        name: '',
        remark: '',
        status: ''
      },
      editFormData: {
        id: '',
        createTime: '',
        updateTime: '',
        name: '',
        remark: '',
        status: ''
      },
      addRules: {
        id: [{ required: true }],
        createTime: [{ required: true }],
        updateTime: [{ required: true }],
        name: [{ required: true }],
        remark: [{ required: true }],
        status: [{ required: true }]
      },
      editRules: {
        id: [{ required: true }],
        createTime: [{ required: true }],
        updateTime: [{ required: true }],
        name: [{ required: true }],
        remark: [{ required: true }],
        status: [{ required: true }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    allRole,
    getList() {
      this.tableLoading = true
      pageData({ current: this.pageWrapper.current, size: this.pageWrapper.size, ...this.queryParams }).then(res => {
        this.setPageWrapper(res.data)
        this.tableData = res.data.records
        this.tableLoading = false
      })
    },
    setEditData(row) {
      Object.keys(this.editFormData).forEach(key => {
        this.editFormData[key] = row[key]
      })
      this.editFormVisible = true
    },
    updateTGroup(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.editFormData).then(res => {
            this.$notify['success']({
              title: res.message
            })
            this.getList()
            this.editFormVisible = false
          })
        }
      })

    },
    createTGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          create(this.addFormData).then(res => {
            this.$notify['success']({
              title: res.message
            })
            this.getList()
            this.addFormVisible = false
          })
        }
      })

    },
    remove(row) {
      removeById(row.id).then(res => {
        this.$notify['success']({
          title: res.message
        })
        this.getList()
      })

    },
    updateGroupRole(){
      bindRoles({roles:this.roles,groupId:this.checkedGroupId}).then(res =>{
        this.$notify({
          type:'success',
          message:res.message
        })
        this.roleVisible = false
      })
    },
    loadGroupRoles(){
      this.roles = []
      loadGroupRole(this.checkedGroupId).then(res =>{
        this.roles = res.data.map(r =>r.roleId);
        this.roleVisible = true;
      })
    }

  }

}
</script>

<style scoped>

</style>
